package com.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import com.util.Workspace;

import java.awt.FlowLayout;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.JComboBox;
import java.awt.GridLayout;
import java.awt.Color;

public class MainApplication {

	public static final String SPLASH = "startupSplashScreen";
	public static final String LOGIN = "loginPanel";
	public static final String DESIGN = "designPanel";
	public static final String WORKSPACE = "workspacePanel";

	Workspace workspaceObj = null;

	private JFrame frame;
	private JTextField userNameTextField;
	private JPasswordField userPasswordField;
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmLogin;
	private JMenuItem mntmLogout;
	private JMenuItem mntmExit;
	private JMenu mnEdit;
	private JPanel startupSplashScreen;
	private JPanel loginPanel;
	private JPanel loginPanel_1;
	private JPanel loginPanel_2;

	private JButton btnLogin;
	private JTextArea txtrWelcomToGorilla;
	private JPanel designPanel;
	private JPanel workspacePanel;
	private JPanel workspacePanelCurrentLib;
	private JLabel lblListOfDesigns;
	private JPanel workspacePanelAddNewLib;
	private JLabel lblAddNewLibrary;
	private JPanel workspacePanelCurrentLib_2;
	private JButton btnEditLibraryDesign;
	private JButton btnDeleteLibraryDesign;
	private JPanel workspacePanelCurrentLib_1;
	private JComboBox comboBoxLibList;
	private JMenu mnHelp;
	private JMenuItem mntmAbout;
	private JPanel panelMainCard;
	private JPanel panelStatus;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainApplication window = new MainApplication();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainApplication() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// MENU BAR CREATION HERE
		menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		mnFile = new JMenu("File");
		mnEdit = new JMenu("Edit");
		menuBar.add(mnFile);
		menuBar.add(mnEdit);

		mntmLogin = new JMenuItem("Login...");
		mntmLogout = new JMenuItem("Logout...");
		mntmExit = new JMenuItem("Exit");

		mnFile.add(mntmLogin);
		mnFile.add(mntmLogout);
		mnFile.add(mntmExit);

		mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		mntmAbout = new JMenuItem("About");
		mnHelp.add(mntmAbout);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));

		panelMainCard = new JPanel(); 
		frame.getContentPane().add(panelMainCard);
		panelMainCard.setLayout(new CardLayout(0, 0));
		
//		CardLayout cl = (CardLayout)frame.getContentPane().get

		
		
		// CREATE CARDS/PANELS AND ADD TO FRAME HERE
		startupSplashScreen = new JPanel();
		panelMainCard.add(startupSplashScreen, "name_12007732070669");

		txtrWelcomToGorilla = new JTextArea();
		txtrWelcomToGorilla
				.setText("Welcom to GoRILLA.  Please login by selecting File > Login.");
		startupSplashScreen.add(txtrWelcomToGorilla);

		loginPanel = new JPanel();
		panelMainCard.add(loginPanel, "name_12012382589990");

		loginPanel.setLayout(new BorderLayout(0, 0));

		loginPanel_1 = new JPanel();
		loginPanel.add(loginPanel_1, BorderLayout.NORTH);
		loginPanel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblUserName = new JLabel("User Name:");
		loginPanel_1.add(lblUserName);

		userNameTextField = new JTextField();
		loginPanel_1.add(userNameTextField);
		userNameTextField.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		loginPanel_1.add(lblPassword);

		userPasswordField = new JPasswordField();
		userPasswordField.setColumns(15);
		loginPanel_1.add(userPasswordField);

		loginPanel_2 = new JPanel();
		loginPanel.add(loginPanel_2, BorderLayout.CENTER);

		btnLogin = new JButton("LOGIN");
		loginPanel_2.add(btnLogin);

		workspacePanel = new JPanel();
		panelMainCard.add(workspacePanel, "name_12017274139817");
		workspacePanel.setLayout(new BorderLayout(0, 0));

		workspacePanelCurrentLib = new JPanel();
		workspacePanel.add(workspacePanelCurrentLib, BorderLayout.WEST);
		workspacePanelCurrentLib.setForeground(new Color(0, 0, 0));
		workspacePanelCurrentLib.setLayout(new BorderLayout(0, 0));

		lblListOfDesigns = new JLabel("List of Current Libraries");
		workspacePanelCurrentLib.add(lblListOfDesigns, BorderLayout.NORTH);

		workspacePanelCurrentLib_1 = new JPanel();
		workspacePanelCurrentLib.add(workspacePanelCurrentLib_1,
				BorderLayout.CENTER);

		comboBoxLibList = new JComboBox();
		workspacePanelCurrentLib_1.add(comboBoxLibList);

		workspacePanelCurrentLib_2 = new JPanel();
		workspacePanelCurrentLib.add(workspacePanelCurrentLib_2,
				BorderLayout.SOUTH);
		workspacePanelCurrentLib_2.setLayout(new BorderLayout(0, 0));

		btnEditLibraryDesign = new JButton("Edit Library");
		workspacePanelCurrentLib_2
				.add(btnEditLibraryDesign, BorderLayout.NORTH);

		btnDeleteLibraryDesign = new JButton("Delete Library");
		workspacePanelCurrentLib_2.add(btnDeleteLibraryDesign,
				BorderLayout.SOUTH);

		workspacePanelAddNewLib = new JPanel();
		workspacePanel.add(workspacePanelAddNewLib, BorderLayout.CENTER);

		lblAddNewLibrary = new JLabel("Add New Library");
		workspacePanelAddNewLib.add(lblAddNewLibrary);

		designPanel = new JPanel();
		panelMainCard.add(designPanel, "name_12020661483513");
		designPanel.setLayout(new BorderLayout(0, 0));

		// Login Button
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (workspaceObj == null) {
					workspaceObj = new Workspace(userNameTextField.getText(),
							new String(userPasswordField.getPassword()));
					if (workspaceObj.isConnected()) {
						// goto design card
						// cl.show(frame.getContentPane(), WORKSPACE);
						if (workspaceObj.getDesigns().keySet() != null)
							for (String name : workspaceObj.getDesigns()
									.keySet()) {
								// add each lib from DB to the list
								comboBoxLibList.addItem(name);
							}
					} else {
						// invalid username/password
						workspaceObj = null;
					}
				} else {
					// alreeady logged in. what now?!
				}
			}
		});

		panelStatus = new JPanel();
		frame.getContentPane().add(panelStatus, BorderLayout.SOUTH);

		// ****ACTIONLISTENERS GO!****

		// File>Login switch to login card
		mntmLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// cl.show(frame.getContentPane(), LOGIN);
			}
		});

		// File>Logout
		mntmLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});

		// File>Exit
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

	}
}
